/*
	This file is part of PropExtractor.

	PropExtractor is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	PropExtractor is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with PropExtractor. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _PEALICESOFTMODEL_H_
#define _PEALICESOFTMODEL_H_

#include "PEFile.h"
#include "PEModel.h"

namespace pe
{

	struct AliceModelZippedHeader
	{
		byte header[4];
		dword padding;
		dword datasize;
		dword unknown1;
	};

	struct AliceModelHeader
	{
		byte header[4];
		dword skin_size;
		dword material_count;
	};

	struct AliceVertexData
	{
		float vx, vy, vz;
		word unknown1;
	};

	struct AliceUVData
	{
		float x, y;
	};

	struct AliceFaceData
	{
		dword vertex_index[3];
		dword uv_index[3];
		dword unknown1[3];
	};

	struct AliceBoneData
	{
		dword id;
		dword parent_id;
		float values[7];
	};

	struct AliceBone
	{
		wchar_t* name;
		AliceBoneData* data;
	};

	struct AliceSkinData1
	{
		word bone_id;
		float weight;
	};

	struct AliceSkin
	{
		AliceSkinData1* data1;
		unsigned int count;

		unsigned int vertex_id;
	};

	struct AliceMaterial
	{
		wchar_t* name;

		wchar_t* texture_diffuse;
		wchar_t* texture_specular;
		unsigned int texture_total;

		unsigned int index;

		AliceMaterial* submaterial;
		unsigned int submaterial_total;
	};

	struct AliceMesh
	{
		wchar_t* name;

		AliceMaterial* material;
		int submaterial;

		AliceVertexData* vertex_data;
		unsigned int vertex_total;

		AliceSkin* skin_data;
		unsigned int skin_total;

		AliceUVData* uv_data1;
		unsigned int uv_total;

		AliceUVData* uv_data2;
		unsigned int uv_total2;

		AliceFaceData* face_data;
		unsigned int face_total;
	};

	struct AliceModelData
	{
		PEStream* handle;
		byte* data;
		unsigned int datasize;
		AliceModelHeader* header;
	};

	struct AliceModel
	{
		PEStream* handle;

		AliceModelHeader* header;

		AliceMaterial* material;
		unsigned int material_total;

		AliceMesh* mesh;
		unsigned int mesh_total;
		unsigned int mesh_filled;

		AliceBone* bone;
		unsigned int bone_total;
	};

	PE_EXPORT int AliceZippedModelReadHeader(AliceModelZippedHeader* a_Target, PEStream* a_Stream);
	PE_EXPORT int AliceZippedModelDecompress(AliceModelData* a_Target, AliceModelZippedHeader* a_Header, PEStream* a_Stream);
	PE_EXPORT int AliceZippedModelToObj(const wchar_t* a_ModelPath, const char* a_Pattern = NULL);

	PE_EXPORT int AliceModelReadHeader(AliceModelHeader* a_Target, PEStream* a_Stream);
	PE_EXPORT int AliceModelReadData(AliceModelData* a_Data, AliceModelHeader* a_Header, PEStream* a_Stream);
	PE_EXPORT int AliceModelDecrypt(AliceModel* a_Target, AliceModelData* a_Data);

	PE_EXPORT int AliceModelDeleteMaterial(AliceMaterial* a_Material);
	PE_EXPORT int AliceModelDeleteMesh(AliceMesh* a_Mesh);
	PE_EXPORT int AliceModelDeleteData(AliceModel* a_Model);

	PE_EXPORT int AliceModelSaveToFile(AliceModelData* a_Data, const wchar_t* a_FilePath, const char* a_Pattern = NULL);

	PE_EXPORT int AliceModelDecryptAndSave(const wchar_t* a_ModelPath, const wchar_t* a_TargetPath, const char* a_Pattern = NULL);
	PE_EXPORT int AliceModelSaveToObj(AliceModel* a_Model, const wchar_t* a_FilePath);

	PE_EXPORT int AliceModelToRaw(PEModelRaw* a_Target, AliceModel* a_Model);

}; // namespace pe

#endif